package com.commerce.controller.back.role;

import com.commerce.authority.AuthType;
import com.commerce.authority.Authority;
import com.commerce.common.JsonResult;
import com.commerce.model.Role;
import com.commerce.page.RolePage;
import com.commerce.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @author wyj
 * @Title:
 * @Description:
 */
@Controller
@RequestMapping(value = "/admin/role" , method = RequestMethod.POST)
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 角色列表
     * @return
     */
    @RequestMapping(value = "list")
    @Authority(AuthType.ROLE_SELECT)
    public String list(){
        return "back/role/list";
    }

    /**
     * 分页查询
     * @param rolePage
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "pageList" )
    @Authority(AuthType.ROLE_SELECT)
    public JsonResult pageList(RolePage rolePage ){
        roleService.selectByPage(rolePage);
        return JsonResult.returnJsonResult("0","",""+rolePage.getTotal(),rolePage.getList());
    }

    /**
     * 编辑页面
     * @param id
     * @return
     */
    @RequestMapping(value = "info")
    @Authority({AuthType.ROLE_INSERT,AuthType.ROLE_UPDATE})
    public String info(Integer id , Model model){
        Role role =null;
        if(id != null && id > 0)
            role = roleService.selectById(id);
        model.addAttribute("role",role);
        return "back/role/info";
    }

    /**
     * 新增
     * @param role
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "addRole")
    @Authority(AuthType.ROLE_INSERT)
    public String addRole(Role role , HttpServletRequest request){
        return roleService.insert(role ,request);
    }

    /**
     * 编辑
     * @param role
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "editRole")
    @Authority(AuthType.ROLE_UPDATE)
    public String editRole(Role role, HttpServletRequest request){
        return roleService.update(role , request);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "delRole")
    @Authority(AuthType.ROLE_DELETE)
    public String delRole(int id){
        return roleService.deleteById(id);
    }
}
